const redisClients = [];
let curIndex = -1;
module.exports = class {
    constructor() {
        this.config = require("../../config");
        this.Redis = require("ioredis");
        this.redisClients = redisClients;
        this.maxConnections = this.config.redis.pool.maxConnections || 1;
    }

    static init() {
        return new this();
    }

    getCurIndex() {
        let _curIndex = curIndex;
        if (_curIndex == this.maxConnections - 1) {
            return 0;
        } else {
            _curIndex++;
        }
        return _curIndex;
    }


    getClient() {

        if (this.redisClients.length < this.maxConnections) {
            let redis = new this.Redis(this.config.redis);
            this.redisClients.push(redis);
        }
        curIndex = this.getCurIndex();
        return this.redisClients[curIndex]; //平均分配客户端
    }
};